package seismosurfer.data;

import java.io.Serializable;
import java.util.List;
import java.util.Vector;

/**
 * Contains the data of the earthquakes that are returned from the server after
 * the execution of a query. This class acts as a cache for the earthquake data,
 * so they can be used in the client-side without needing to communicate with
 * the server.
 */
public class QuakeClientData implements Serializable {
    
    private static final long serialVersionUID = -1468763869035564612L;

    private static List quakeResults = new Vector(1024, 256);

    private static int rowCount = 0;

    private static long maxTime = Long.MIN_VALUE;

    private static long minTime = Long.MAX_VALUE;

    private static double minMagnitude = Double.MAX_VALUE;

    private static double maxMagnitude = Double.MIN_VALUE;

    private static double minDepth = Double.MAX_VALUE;

    private static double maxDepth = Double.MIN_VALUE;

    private static double minLatitude = 90;

    private static double maxLatitude = -90;

    private static double minLongitude = 180;

    private static double maxLongitude = -180;

    /**
     * Default constructor.
     */
    public QuakeClientData() {

    }

    /**
     * Deletes all the quakes data and sets the min/max values 
     * for the quakes parameters (magnitude, depth, e.t.c.).
     */
    public static void clear() {

        quakeResults.clear();

        rowCount = 0;

        maxTime = Long.MIN_VALUE;
        minTime = Long.MAX_VALUE;

        minMagnitude = Double.MAX_VALUE;
        maxMagnitude = Double.MIN_VALUE;

        minDepth = Double.MAX_VALUE;
        maxDepth = Double.MIN_VALUE;

        minLatitude = 90;
        maxLatitude = -90;

        minLongitude = 180;
        maxLongitude = -180;

    }

    /**
     */
    public static List getQuakeResults() {
        return quakeResults;
    }

    /**
     */
    public static int getRowCount() {
        return rowCount;
    }

    /**
     */
    public static double getMaxMagnitude() {
        return maxMagnitude;
    }

    /**
     */
    public static double getMinMagnitude() {
        return minMagnitude;
    }

    /**
     */
    public static double getMaxDepth() {
        return maxDepth;
    }

    /**
     */
    public static double getMinDepth() {
        return minDepth;
    }

    /**
     */
    public static double getMaxLatitude() {
        return maxLatitude;
    }

    /**
     */
    public static double getMinLatitude() {
        return minLatitude;
    }

    /**
     */
    public static double getMaxLongitude() {
        return maxLongitude;
    }

    /**
     */
    public static double getMinLongitude() {
        return minLongitude;
    }

    /**
     */
    public static long getMaxTime() {
        return maxTime;
    }

    /**
     */
    public static long getMinTime() {
        return minTime;
    }

    // --------------------------------------------------------------------------//

    /**
     * Modifies the min/max values based on the list 
     * of earthquake data that it gets.
     * 
     * @param qdList  the list with the erthquake data
     */
    public static void setQuakeResults(List qdList) {

        quakeResults = qdList;

        if (quakeResults == null) {
            return;
        }

        // Load Data in static variables

        rowCount = quakeResults.size();

        double tempd;
        long templ;

        for (int i = 0; i < rowCount; i++) {

            QuakeData qd = (QuakeData) quakeResults.get(i);

            templ = qd.getDateTime();
            if (templ > maxTime) {
                maxTime = templ;
            }
            if (templ < minTime) {
                minTime = templ;

            }
            tempd = qd.getDepth();
            if (tempd > maxDepth) {
                maxDepth = tempd;
            }
            if (tempd < minDepth) {
                minDepth = tempd;

            }
            tempd = qd.getMagnitude();
            if (tempd > maxMagnitude) {
                maxMagnitude = tempd;
            }
            if (tempd < minMagnitude) {
                minMagnitude = tempd;

            }
            tempd = qd.getLongitude();
            if (tempd > maxLongitude) {
                maxLongitude = tempd;
            }
            if (tempd < minLongitude) {
                minLongitude = tempd;

            }
            tempd = qd.getLatitude();
            if (tempd > maxLatitude) {
                maxLatitude = tempd;
            }
            if (tempd < minLatitude) {
                minLatitude = tempd;

            }
        }
    }

}
